package com.banggood.upms.web;

import com.banggood.silk.web.api.common.PagedResult;
import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.exception.ExceptionEnum;
import com.banggood.upms.bean.po.TreeInfo;
import com.banggood.upms.bean.vo.search.TreeInfoSearchCriteria;
import com.banggood.upms.bean.vo.validation.AddGroup;
import com.banggood.upms.bean.vo.validation.SearchGroup;
import com.banggood.upms.common.constant.UpmsConstant;
import com.banggood.upms.service.TreeInfoService;
import com.banggood.upms.common.component.CommonComponent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @author XuChuangFeng
 * @date 2017/10/12
 */
@RestController
@RequestMapping("/tree-info")
@Api(value = "树信息管理", description = "树基本信息操作。")
public class TreeInfoController {

    @Autowired
    private TreeInfoService treeInfoService;
    @Autowired
    private CommonComponent commonComponent;

    @PostMapping("/search")
    @ApiOperation(value = "树信息检索", notes = "分页查询upms中的树信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "treeType", value = "树类型，有唯一约束", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    public PagedResult<TreeInfo> search(@Validated({SearchGroup.class}) TreeInfoSearchCriteria criteria) throws Exception {
        PageHelper.startPage(criteria.getPageNum(), criteria.getRows());
        List<TreeInfo> treeInfos = treeInfoService.selectByCriteria(criteria);
        long total = new PageInfo<>(treeInfos).getTotal();
        PagedResult<TreeInfo> result = new PagedResult<>(treeInfos, total);
        return result;
    }

    /**
     * @param treeInfo
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建树", notes = "只是增加树的基本信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "treeType", value = "树类型，有唯一约束", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "树类别已经存在"),
            @ApiResponse(code = 5000, message = "操作失败")
    })
    public String createTree(@Validated({AddGroup.class}) TreeInfo treeInfo) throws Exception {
        // 检索DB中是否已经存在此类别的树,不允许出现重复的treeType
        if(treeInfoService.ifTreeTypeExists(treeInfo.getTreeType())) {
            throw new SkbException(ExceptionEnum.TREE_TYPE_EXISTS.getCode(), ExceptionEnum.TREE_TYPE_EXISTS.getMessage());
        }
        // 执行插入操作
        String id = commonComponent.generateId();
        treeInfo.setTreeInfoId(id);
        int count = treeInfoService.insert(treeInfo);
        if (count == 0) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return id;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新备注", notes = "目前只允许更新备注。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 5000, message = "操作失败!")
    })
    public String updateByPrimaryKey(TreeInfo treeInfo) throws Exception {
        int count = treeInfoService.updateByPrimaryKey(treeInfo);
        if(count != 1) {
            throw new SkbException(ExceptionEnum.OPERATION_FAILED.getCode(), ExceptionEnum.OPERATION_FAILED.getMessage());
        }
        return UpmsConstant.SUCCESS;
    }
}
